﻿using Comm100.Framework.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using Bot.Domain.Bo;
using Castle.Windsor;
using Bot.Domain.Specifications;
using Comm100.Framework.Domain.Services;

namespace Bot.Domain.Categories.Service
{
    public class AgentAssistDomainService : DomainServiceBase, IAgentAssistDomainService
    {
        private IWindsorContainer _container;
        private readonly IRepository<Guid, AgentAssist> _repository;

        public AgentAssistDomainService(IRepository<Guid, AgentAssist> repository, IWindsorContainer container)
        {
            this._repository = repository;
            this._container = container;
        }

        public IEnumerable<AgentAssist> List()
        {
            return _repository.ListAll();
        }

        public void Delete(Guid id)
        {
            AgentAssist agentAssist = _repository.Get(id);

            _repository.Delete(agentAssist);

        }
        public AgentAssist Create(AgentAssist assist)
        {
            if (assist.IfIncludeChatBot && assist.SelectedChatbots != null && assist.SelectedChatbots.Count > 0)
            {
                ISelectedChatBotDomainService botDomainService = _container.Resolve<ISelectedChatBotDomainService>();
                foreach (var botId in assist.SelectedChatbots)
                {
                    botDomainService.Create(new SelectedChatBot()
                    {
                        Id = Guid.NewGuid(),
                        AgentAssistId = assist.Id,
                        ChatBotId = botId
                    });
                }
            }
            if (assist.IfIncludeKnowledgeBase && assist.SelectedKnowledgeBases != null && assist.SelectedKnowledgeBases.Count > 0)
            {
                ISelectedKBDomainService kbDomainService = _container.Resolve<ISelectedKBDomainService>();
                foreach (var kbId in assist.SelectedKnowledgeBases)
                {
                    kbDomainService.Create(new SelectedKB()
                    {
                        Id = Guid.NewGuid(),
                        AgentAssistId = assist.Id,
                        KBId = kbId
                    });
                }
            }
            _repository.Create(assist);
            return Get(assist.Id);
        }

        public AgentAssist Get(Guid id)
        {
            var result = _repository.Get(id);
            if (result.IfIncludeChatBot)
            {
                ISelectedChatBotDomainService botDomainService = _container.Resolve<ISelectedChatBotDomainService>();
                result.SelectedChatbots = botDomainService.GetByAgentAssistId(id).Select(a => a.ChatBotId).ToList();
                result.SelectedKnowledgeBases = null;
            }
            if (result.IfIncludeKnowledgeBase)
            {
                ISelectedKBDomainService kbDomainService = _container.Resolve<ISelectedKBDomainService>();
                result.SelectedKnowledgeBases = kbDomainService.GetByAgentAssistId(id).Select(a => a.KBId).ToList();
                result.SelectedChatbots = null;
            }
            return result;
        }

        public AgentAssist Update(AgentAssistUpdateBo bo)
        {
            var assist = _repository.ListAll().FirstOrDefault();

            assist.HighConfidenceScore = bo.HighConfidenceScore;
            assist.IfAddUnrecognizedQuestionsToLearning = bo.IfAddUnrecognizedQuestionsToLearning;
            assist.IfAddVisitorQuestionAsSimilarQuestion = bo.IfAddVisitorQuestionAsSimilarQuestion;
            assist.IfIncludeCannedMessage = bo.IfIncludeCannedMessage;
            assist.IfIncludeChatBot = bo.IfIncludeChatBot;
            assist.IfIncludeKnowledgeBase = bo.IfIncludeKnowledgeBase;
            assist.IsEnabled = bo.IsEnabled;
            //assist.Language = bo.Language;
            assist.MaximumSuggestionNumber = bo.MaximumSuggestionNumber;
            assist.TextBeforeKBArticle = bo.TextBeforeKBArticle;

            if (bo.IfIncludeChatBot && bo.SelectedChatbots != null && bo.SelectedChatbots.Count > 0)
            {
                ISelectedChatBotDomainService botDomainService = _container.Resolve<ISelectedChatBotDomainService>();
                var botIds = botDomainService.GetByAgentAssistId(assist.Id);
                foreach (var botId in bo.SelectedChatbots)
                {
                    if (botIds.FirstOrDefault(a => a.ChatBotId == botId) == null)
                    {
                        //create
                        botDomainService.Create(new SelectedChatBot()
                        {
                            Id = Guid.NewGuid(),
                            AgentAssistId = assist.Id,
                            ChatBotId = botId
                        });
                    }
                }
                //delete
                foreach (var deletedItem in botIds)
                {
                    if (!bo.SelectedChatbots.Contains(deletedItem.ChatBotId))
                    {
                        botDomainService.Delete(deletedItem.Id);
                    }
                }
            }
            if (bo.IfIncludeKnowledgeBase && bo.SelectedKnowledgeBases != null && bo.SelectedKnowledgeBases.Count > 0)
            {
                ISelectedKBDomainService kbDomainService = _container.Resolve<ISelectedKBDomainService>();
                var kbIds = kbDomainService.GetByAgentAssistId(assist.Id);
                foreach (var kbId in bo.SelectedKnowledgeBases)
                {
                    if (kbIds.FirstOrDefault(a => a.KBId == kbId) == null)
                    {
                        //create
                        kbDomainService.Create(new SelectedKB()
                        {
                            Id = Guid.NewGuid(),
                            AgentAssistId = assist.Id,
                            KBId = kbId
                        });
                    }
                }
                //delete
                foreach (var deletedItem in kbIds)
                {
                    if (!bo.SelectedKnowledgeBases.Contains(deletedItem.KBId))
                    {
                        kbDomainService.Delete(deletedItem.Id);
                    }
                }
            }
            return Get(assist.Id);
        }

        public AgentAssist GetBySiteId()
        {
            var result = _repository.ListAll().FirstOrDefault();
            if (result != null)
            {
                result = Get(result.Id);
            }
            return result;
        }
    }
}
